package GUI.Ventas;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.ComboVO;
import article.exceptions.OutOfStockIngredientsException;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.TableItem;

public class AgregarArticulosDelivery extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text p1;
	private Text p2;
	private Text p3;
	private Text p4;
	private Text d1;
	private Text d2;
	private Text d3;
	private Text d4;
	Shell padre;
	int mesa;
	ArrayList <ArticleVO> pedidos = new ArrayList<ArticleVO>();
	ArrayList <String> descripciones = new ArrayList<String>();
	SaleVO s;
	OrderVO orden;
	int opcion;
	private Table table;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarArticulosDelivery(Shell parent, int style, SaleVO sale, int m, int op, OrderVO o) {
		super(parent, style);
		setText("SWT Dialog");
		padre=parent;
		s=sale;
		mesa=m;
		orden=o;
		opcion=op;
		System.out.println("opcion" + opcion);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(667, 463);
		shell.setText(getText());
		
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		final ArticleVO [] arts = uso.getArticles();
		final Combo c1 = new Combo(shell, SWT.NONE);
		c1.setBounds(63, 227, 112, 22);
		
		final Combo c2 = new Combo(shell, SWT.NONE);
		c2.setBounds(63, 272, 112, 22);
		c2.setVisible(false);
		
		final Combo c3 = new Combo(shell, SWT.NONE);
		c3.setBounds(63, 321, 112, 22);
		c3.setVisible(false);
		
		final Combo c4 = new Combo(shell, SWT.NONE);
		c4.setBounds(63, 369, 112, 22);
		c4.setVisible(false);
		
		p1 = new Text(shell, SWT.BORDER);
		p1.setBounds(210, 230, 64, 19);
		
		p2 = new Text(shell, SWT.BORDER);
		p2.setBounds(210, 275, 64, 19);
		p2.setVisible(false);
		
		p3 = new Text(shell, SWT.BORDER);
		p3.setBounds(210, 324, 64, 19);
		p3.setVisible(false);
		
		p4 = new Text(shell, SWT.BORDER);
		p4.setBounds(210, 372, 64, 19);
		p4.setVisible(false);
		
		d1 = new Text(shell, SWT.BORDER);
		d1.setBounds(309, 230, 137, 19);
		
		d2 = new Text(shell, SWT.BORDER);
		d2.setBounds(309, 275, 137, 19);
		d2.setVisible(false);
		
		d3 = new Text(shell, SWT.BORDER);
		d3.setBounds(309, 324, 137, 19);
		d3.setVisible(false);
		
		d4 = new Text(shell, SWT.BORDER);
		d4.setBounds(309, 372, 137, 19);
		d4.setVisible(false);
		
		Button s1 = new Button(shell, SWT.NONE);
	
		s1.setBounds(462, 221, 94, 28);
		s1.setText("Agregar");
		
		final Button s2 = new Button(shell, SWT.NONE);
	
		s2.setBounds(462, 266, 94, 28);
		s2.setText("Agregar");
		s2.setVisible(false);
		
		final Button s3 = new Button(shell, SWT.NONE);
		
		s3.setBounds(462, 321, 94, 28);
		s3.setText("Agregar");
		s3.setVisible(false);
		
		final Button s4 = new Button(shell, SWT.NONE);
		s4.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				int numero= Integer.parseInt(p4.getText());
				
				String nuevo = c4.getText();
				
				if(nuevo!=null){
					for (int i=0;i<arts.length-1;i++){
						if (arts[i].getName().equalsIgnoreCase(nuevo)){
							for(int j= 0; j<numero;j++){
								pedidos.add(arts[i]);
								descripciones.add(d4.getText());
							}
							
						}
			}
		}
			}
			}
		);
		
		
		s4.setBounds(462, 369, 94, 28);
		s4.setText("Agregar");
		s4.setVisible(false);
		
		Button aregar = new Button(shell, SWT.NONE);
		aregar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (opcion ==6){
					OrderVO nuevaOrden = new OrderVO (pedidos, descripciones);
					
					SaleProxyMgt a = ServiceFacade.getInstance().getSaleProxyMgt();
					try {
						
						try {
							a.addOrder(nuevaOrden, s.getSaleKey());
						} catch (OutOfStockIngredientsException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} catch (RemoteException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (ProxyException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (EmptySaleExcpetion e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					//shell.setVisible(false);
					VetnasAbiertasDelivery v = new VetnasAbiertasDelivery(shell, shell.getStyle(), 1);
					try {
						v.open();
						shell.dispose();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if(opcion==5){
					OrderVO nuevaOrden = new OrderVO (pedidos, descripciones);
					
					SaleProxyMgt a = ServiceFacade.getInstance().getSaleProxyMgt();
					try {
						
						try {
							a.addOrder(nuevaOrden, s.getSaleKey());
						} catch (OutOfStockIngredientsException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} catch (RemoteException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (ProxyException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (EmptySaleExcpetion e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					shell.setVisible(false);
					VentasAbiertasMesas v = new VentasAbiertasMesas(shell, shell.getStyle(), 1);
					try {
						v.open();
						shell.dispose();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if(opcion==4){
					OrderVO nuevaOrden = new OrderVO (pedidos, descripciones);
				
					SaleProxyMgt a = ServiceFacade.getInstance().getSaleProxyMgt();
					try {
						
						try {
							a.addOrder(nuevaOrden, s.getSaleKey());
						} catch (OutOfStockIngredientsException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} catch (RemoteException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (ProxyException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (EmptySaleExcpetion e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					//shell.setVisible(false);
					VentasAbiertasMostrador v = new VentasAbiertasMostrador(shell, shell.getStyle(), 0);
					try {
						v.open();
						shell.dispose();
					} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
				}
				if(opcion==2){
					ArrayList<ArticleVO> a = orden.getArts();
					ArrayList<String> d = orden.getEspecif();
					for(int i =0; i<pedidos.size();i++){
						a.add(pedidos.get(i));
						d.add(descripciones.get(i));
						
					}
					orden.setArts(a);
					orden.setEspecif(d);
					AgregarVentaMesa agregar = new AgregarVentaMesa (shell, shell.getStyle(),orden);
					
					agregar.open();
					shell.dispose();
					
				}
				
				if(opcion==1){
				
					
					
				ArrayList<ArticleVO> a = orden.getArts();
				ArrayList<String> d = orden.getEspecif();
				for(int i =0; i<pedidos.size();i++){
					a.add(pedidos.get(i));
					d.add(descripciones.get(i));
					
				}
				orden.setArts(a);
				orden.setEspecif(d);
				AgregarVentaMostrador agregar = new AgregarVentaMostrador (shell, shell.getStyle(), orden);
				shell.setVisible(false);
				agregar.open();
				
			}
			if(opcion==0){
				ArrayList<ArticleVO> a = orden.getArts();
				ArrayList<String> d = orden.getEspecif();
				for(int i =0; i<pedidos.size();i++){
					a.add(pedidos.get(i));
					d.add(descripciones.get(i));
					
				}
				orden.setArts(a);
				orden.setEspecif(d);
				NuevaVentaDelivery nueva= new NuevaVentaDelivery (shell, shell.getStyle(), pedidos, descripciones, orden,null);
				shell.setVisible(false);
				nueva.open();
			}}
			});
		
		
		s1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int numero= Integer.parseInt(p1.getText());
				c2.setVisible(true);
				p2.setVisible(true);
				d2.setVisible(true);
				s2.setVisible(true);
				String nuevo = c1.getText();
				
				if(nuevo!=null){
					System.out.println("no es nulo");
					for (int i=0;i<arts.length;i++){
						if (arts[i].getName().equalsIgnoreCase(nuevo)){
							for(int j= 0; j<numero;j++){
								pedidos.add(arts[i]);
								descripciones.add(d1.getText());
							}
							
						}
			}
		}}});
		s2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int numero= Integer.parseInt(p2.getText());
				c3.setVisible(true);
				p3.setVisible(true);
				d3.setVisible(true);
				s3.setVisible(true);
				String nuevo = c2.getText();
				
				if(nuevo!=null){
					for (int i=0;i<arts.length-1;i++){
						if (arts[i].getName().equalsIgnoreCase(nuevo)){
							for(int j= 0; j<numero;j++){
								pedidos.add(arts[i]);
								descripciones.add(d2.getText());
							}
							
						}
			}
		}
			}
		});
		s3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int numero= Integer.parseInt(p2.getText());
				c4.setVisible(true);
				p4.setVisible(true);
				d4.setVisible(true);
				s4.setVisible(true);
				String nuevo = c3.getText();
				
				if(nuevo!=null){
					for (int i=0;i<arts.length-1;i++){
						if (arts[i].getName().equalsIgnoreCase(nuevo)){
							for(int j= 0; j<numero;j++){
								pedidos.add(arts[i]);
								descripciones.add(d3.getText());
								shell.close();
							}
							
						}
			}
		}
			
			}
		});
		aregar.setBounds(397, 403, 247, 28);
		aregar.setText("Agregar Articulos a pedido");
		
		Label lblArticulo = new Label(shell, SWT.NONE);
		lblArticulo.setBounds(55, 188, 59, 14);
		lblArticulo.setText("Articulo");
		
		Label lblPrecio = new Label(shell, SWT.NONE);
		lblPrecio.setBounds(202, 188, 59, 14);
		lblPrecio.setText("Cantidad");
		
		Label lblEspecificaciones = new Label(shell, SWT.NONE);
		lblEspecificaciones.setBounds(301, 188, 94, 14);
		lblEspecificaciones.setText("Especificaciones");
		
		Label lblCombosDelDia = new Label(shell, SWT.NONE);
		lblCombosDelDia.setBounds(10, 21, 133, 14);
		lblCombosDelDia.setText("Combos del Dia");
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 41, 634, 116);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNombre = new TableColumn(table, SWT.NONE);
		tblclmnNombre.setWidth(118);
		tblclmnNombre.setText("Nombre");
		
		TableColumn tblclmnArticulos = new TableColumn(table, SWT.NONE);
		tblclmnArticulos.setWidth(229);
		tblclmnArticulos.setText("Articulos");
		
		TableColumn tblclmnDescripcion = new TableColumn(table, SWT.NONE);
		tblclmnDescripcion.setWidth(208);
		tblclmnDescripcion.setText("Descripcion");
		
		TableColumn tblclmnPrecio = new TableColumn(table, SWT.NONE);
		tblclmnPrecio.setWidth(100);
		tblclmnPrecio.setText("Precio");
		
		
		
		Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 163, 634, 19);
		
		ComboVO [] combos = uso.getCombos();
		if(combos==null){
			
		}
		else{
			
		
		for(int l=0;l<combos.length;l++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(0, combos[l].getName());
			String m="";
			for(int h = 0; h<combos[l].getArts().size();h++){
				m=m + combos[l].getArts().get(h).getName();
			}
			tableItem.setText(1,m);
			tableItem.setText(2,combos[l].getDesc());
			tableItem.setText(3, String.valueOf(combos[l].getPrice()));
		}
		}
		for(int i=0;i<arts.length;i++){
			c1.add(arts[i].getName());
			c2.add(arts[i].getName());
			c3.add(arts[i].getName());
			c4.add(arts[i].getName());
		}

	}
}
